//
//  JDAdvertLaunchViewController.swift
//  DragonWallet
//
//  Created by volantPig on 2024/8/23.
//

import UIKit

class JDAdvertLaunchViewController: UIViewController {

    var currentWindow: UIWindow?
    
    var advertLaunchCompleted:((Bool)->Void)?
    
    var mainCollectionView: UICollectionView?
    
    var isChecking: Bool = true
    
    var imageNameArray: [String] = ["launch_Image_one","launch_Image_two","launch_Image_three","launch_Image_four"]
    
    let popupView = JDPPPopupView()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.setupConfig()
        self.setupUIElements()
        // Do any additional setup after loading the view.
        
//        let popupView = JDPPPopupView()
//        popupView.modalPresentationStyle = .custom
//        popupView.modalTransitionStyle = .crossDissolve
//        self.presentVC(popupView)
        
        self.view.addSubview(self.popupView.view)
        
        
    }
    
    func setupConfig() {
        self.view.backgroundColor = .commonColor()
    }
    
    func setupUIElements() {
        let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = .horizontal
        
        self.mainCollectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
        self.view.addSubview(self.mainCollectionView!)
        self.mainCollectionView?.snp.makeConstraints({ make in
            make.top.bottom.left.right.equalTo(self.view)
        })
        self.mainCollectionView?.delegate = self
        self.mainCollectionView?.dataSource = self
        self.mainCollectionView?.isPagingEnabled = true
        self.mainCollectionView?.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "myCell")
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}

extension JDAdvertLaunchViewController: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.imageNameArray.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "myCell", for: indexPath)
        
        let currentImageName = self.imageNameArray[indexPath.row]
        
        let imgV = UIImageView()
        cell.contentView.addSubview(imgV)
        imgV.snp.makeConstraints { make in
            make.left.top.right.bottom.equalTo(cell.contentView)
        }
//        imgV.backgroundColor = .customRandomColor().0
        imgV.image = UIImage(named: currentImageName)
        
        return cell

    }

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        printLog("点击:\(indexPath.row)")
    }
     
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 0.001
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 0.001
    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSizeMake(screenWidth, screenHeight)
    }
    
}

extension JDAdvertLaunchViewController: UIScrollViewDelegate {
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        let offsetX = scrollView.contentOffset.x
        print("当前偏移量:\(offsetX)")
        let cha = offsetX-screenWidth*CGFloat(self.imageNameArray.count-1)
        if cha > 50 && self.isChecking {
            self.isChecking = false
            
            if let userWindow = self.currentWindow {
                UIView.transition(with: userWindow, duration: 0.5, options: .curveLinear, animations: {
                    UserDefaults.standard.setValue(true, forKey: "hadShowAdvert")
                    let tabbarVC = JDTabBarController()
                    self.currentWindow?.rootViewController = tabbarVC
                }, completion: nil)
            }
        }
    }
}
